﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchFlight1 : System.Web.UI.Page
{
    FlightBAL _flightBAL;
    protected void Page_Load(object sender, EventArgs e)
    {
        _flightBAL = new FlightBAL();
        if (Session["requestFromCity"] == null || Session["requestToCity"] == null || Session["DepartDates"] == null)
        {
            Response.Redirect("~/SearchFlight.aspx");
        }
        if (!Page.IsPostBack)
        {
            Session["origin"] = null;
            Session["destination"] = null;

            List<DateTime> DepartTime = (List<DateTime>)Session["DepartDates"];
            lblDepartTime.Text = "Depart Date : ";
            foreach (DateTime date in DepartTime)
            {
                lblDepartTime.Text += (date.DayOfWeek.ToString() + " " + date.Date.ToString().Remove(10) + " , ");
            }
            if (Session["ReturnDates"] != null)
            {
                lblFlight.Text = "Round Trip";
                List<DateTime> ReturnTime = (List<DateTime>)Session["ReturnDates"];
                lblReturnTime.Text = "Return Date : ";
                foreach (DateTime date in ReturnTime)
                {
                    lblReturnTime.Text += (date.DayOfWeek.ToString() + " " + date.Date.ToString().Remove(10) + " , ");
                }
            }
            else
            {
                lblFlight.Text = "One Way";
            }
            lblAdult.Text = Convert.ToString((Int32)Session["Adult"]);
            lblSenior.Text = Convert.ToString((Int32)Session["Senior"]);
            lblChildren.Text = Convert.ToString((Int32)Session["Child"]);
        }
        List<Location> fromRequestList = (List<Location>)Session["requestFromCity"];
        List<Location> toRequestList = (List<Location>)Session["requestToCity"];
        #region From
        if (fromRequestList.Count == 1)
        {
            if (!Page.IsPostBack)
            {
                lblFrom.Text = fromRequestList[0].FullName;
                if (checkLocation(fromRequestList[0]))
                {
                    Session["origin"] = _flightBAL.GetLocationByName(fromRequestList[0].Name, fromRequestList[0].Country);
                }
            }
            else
            {
                checkLocation(fromRequestList[0]);
            }
        }
        else if ((fromRequestList.Count > 1))
        {
            lblFrom.Text = fromRequestList[0].Name + "," + fromRequestList[0].Country;
            if (!Page.IsPostBack)
            {
                PlaceHolder1.Visible = true;
                ddlFromRequest.Items.Clear();
                for (int i = 0; i < fromRequestList.Count; i++)
                {
                    ddlFromRequest.Items.Add(new ListItem(fromRequestList[i].FullName, i.ToString()));
                }
            }
        }
        else
        {
            lblFrom.Text = "place not found."; lblFrom.ForeColor = System.Drawing.Color.Red;
        }
        #endregion

        #region To
        if (toRequestList.Count == 1)
        {
            if (!Page.IsPostBack)
            {
                lblTo.Text = toRequestList[0].FullName;
                if (checkLocation2(toRequestList[0]))
                {
                    Session["destination"] = _flightBAL.GetLocationByName(toRequestList[0].Name, toRequestList[0].Country);
                }
            }
            else
            {
                checkLocation2(toRequestList[0]);
            }
        }
        else if ((toRequestList.Count > 1))
        {
            lblTo.Text = toRequestList[0].Name + "," + toRequestList[0].Country;
            if (!Page.IsPostBack)
            {
                PlaceHolder3.Visible = true;
                ddlToRequest.Items.Clear();
                for (int i = 0; i < toRequestList.Count; i++)
                {
                    ddlToRequest.Items.Add(new ListItem(toRequestList[i].FullName, i.ToString()));
                }
            }
        }
        else
        {
            lblTo.Text = "place not found."; lblFrom.ForeColor = System.Drawing.Color.Red;
        }
        #endregion
    }

    private bool checkLocation(Location location)
    {
        if (_flightBAL.PlaceIsKnown(location))
        {
            lblFrom.Text = location.FullName;
            PlaceHolder2.Visible = false;
            return true;
        }
        else
        {
            List<Location> _nearestLocations = _flightBAL.GetNearestCities(location);
            PlaceHolder2.Visible = true;
            Button2.Visible = true;
            Button2.Enabled = true;
            ddlFromAirport.Items.Clear();
            for (int i = 0; i < _nearestLocations.Count; i++)
            {
                string distance = Convert.ToString(_flightBAL.calDistance(location, _nearestLocations[i]));
                ddlFromAirport.Items.Add(new ListItem(_nearestLocations[i].FullName + "---Distance : " + distance + " km", _nearestLocations[i].ID.ToString()));
            }
            return false;
        }
    }
    private bool checkLocation2(Location location)
    {
        if (_flightBAL.PlaceIsKnown(location))
        {
            lblTo.Text = location.FullName;
            PlaceHolder4.Visible = false;
            return true;
        }
        else
        {
            List<Location> _nearestLocations = _flightBAL.GetNearestCities(location);
            PlaceHolder4.Visible = true;
            Button3.Visible = true;
            Button3.Enabled = true;
            ddlToAirport.Items.Clear();
            for (int i = 0; i < _nearestLocations.Count; i++)
            {
                string distance = Convert.ToString(_flightBAL.calDistance(location, _nearestLocations[i]));
                ddlToAirport.Items.Add(new ListItem(_nearestLocations[i].FullName + "---Distance : " + distance + " km", _nearestLocations[i].ID.ToString()));
            }
            return false;
        }
    }

    protected void ddlFromRequest_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<Location> fromRequestList = (List<Location>)Session["requestFromCity"];
        Location _loc = fromRequestList[Convert.ToInt32(ddlFromRequest.SelectedValue)];
        lblFrom.Text = _loc.FullName;
        if (checkLocation(_loc))
        {
            BtnAccept1.Enabled = true;
            BtnAccept1.Visible = true;
        }
        else
        {
            BtnAccept1.Enabled = false;
            BtnAccept1.Visible = false;
        }
    }
    protected void BtnAccept1_Click(object sender, EventArgs e)
    {
        List<Location> fromRequestList = (List<Location>)Session["requestFromCity"];
        Location _loc = fromRequestList[Convert.ToInt32(ddlFromRequest.SelectedValue)];
        fromRequestList.Clear();
        fromRequestList.Add(_loc);
        if (_flightBAL.PlaceIsKnown(_loc))
        {
            Session["origin"] = _flightBAL.GetLocationByName(_loc.Name, _loc.Country);
        }
        Session["requestFromCity"] = fromRequestList;
        Response.Redirect("~/SearchFlight1.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        List<Location> fromRequestList = (List<Location>)Session["requestFromCity"];
        Location _loc = _flightBAL.GetLocationByID(Convert.ToInt32(ddlFromAirport.SelectedValue));
        fromRequestList.Clear();
        fromRequestList.Add(_loc);
        Session["requestFromCity"] = fromRequestList;
        Session["origin"] = _loc;
        Response.Redirect("~/SearchFlight1.aspx");
    }

    protected void ddlToRequest_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<Location> toRequestList = (List<Location>)Session["requestToCity"];
        Location _loc = toRequestList[Convert.ToInt32(ddlToRequest.SelectedValue)];
        lblTo.Text = _loc.FullName;
        if (checkLocation2(_loc))
        {
            BtnAccept2.Enabled = true;
            BtnAccept2.Visible = true;
        }
        else
        {
            BtnAccept2.Enabled = false;
            BtnAccept2.Visible = false;
        }
    }
    protected void BtnAccept2_Click(object sender, EventArgs e)
    {
        List<Location> toRequestList = (List<Location>)Session["requestToCity"];
        Location _loc = toRequestList[Convert.ToInt32(ddlToRequest.SelectedValue)];
        toRequestList.Clear();
        toRequestList.Add(_loc);
        if (_flightBAL.PlaceIsKnown(_loc))
        {
            Session["destination"] = _flightBAL.GetLocationByName(_loc.Name, _loc.Country);
        }
        Session["requestToCity"] = toRequestList;
        Response.Redirect("~/SearchFlight1.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        List<Location> toRequestList = (List<Location>)Session["requestToCity"];
        Location _loc = _flightBAL.GetLocationByID(Convert.ToInt32(ddlToAirport.SelectedValue));
        Session["destination"] = _loc;
        toRequestList.Clear();
        toRequestList.Add(_loc);
        Session["requestToCity"] = toRequestList;
        Response.Redirect("~/SearchFlight1.aspx");
    }
    protected void btnContinue_Load(object sender, EventArgs e)
    {
        if (Session["origin"] == null && Session["destination"] == null)
        {
            btnContinue.Enabled = false;
        }
        else
        {
            btnContinue.Enabled = true;
        }
    }
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/SearchFlight2.aspx");
    }
}
